package com.example.userregist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.io.Serializable;

//用于封装装备信息的实体类
//实现一个可序列化接口
public class ItemInfo implements Serializable {

    private String name;
    private int Hp;
    private int attack;
    private int speed;

    public ItemInfo(String name, int hp, int attack, int speed) {
        this.name = name;
        this.Hp = hp;
        this.attack = attack;
        this.speed = speed;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return Hp;
    }

    public void setHp(int hp) {
        Hp = hp;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    //创建数据库
    public static class ContactDatabaseHelper extends SQLiteOpenHelper
    {
        private Context context;
        //上下文？
        public ContactDatabaseHelper(Context context)
        {
            super(context,"User.db",null,1);
            this.context=context;
        }
        //运行一次，创建数据表
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "name VARCHAR(20),password VARCHAR(20))");
        }
        //更新数据表
        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
        //插入
        public long insert(String name,String password)
        {
            //获得对数据库操作的权限（读写）
            SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();

            ContentValues values=new ContentValues();
            values.put("name",name);
            values.put("password",password);
            //获得插入数据的id
            long id=sqLiteDatabase.insert("information",null,values);
            if(id!=0)
            {
                Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
            }
            //关闭数据库连接
            sqLiteDatabase.close();
            return id;
        }
        //查询
        public String queryByName(String name)
        {
            //获得对数据库操作的权限（可读）
            SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
            Cursor cursor= sqLiteDatabase.query("information",null,"name=?",
                    new String[]{name},null,null,null);
            if(cursor.getCount()==0)
            {
                //关闭指针
                cursor.close();
                //关闭数据库连接
                sqLiteDatabase.close();
                return "NULL";
            }
            else
            {
                //指针  指到动到第一条数据那
                cursor.moveToFirst();
                //返回列的下标值
                String result=cursor.getString(2);
                cursor.close();
                sqLiteDatabase.close();
                return result;
            }
        }


    }
}
